﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BL.Service;
using BL.Entity;

namespace LTO
{
    /// <summary>
    /// Interaction logic for ViewOver1.xaml
    /// </summary>
    public partial class ViewOver1 : Window
    {
        tbl_NumberServer Numberservice = new tbl_NumberServer();
        tbl_PayService PayService = new tbl_PayService();
        List<ShowNumberDTO> showOver;
        List<ShowNumberDTO> showUnit;
        List<ShowNumberDTO> showDecimal;
        List<ShowNumberDTO> showHundred;
        Pay limit;
        decimal LimitNum = 0;
        decimal minSt = 0;
        decimal minEn = 0;
        string NumberType;
        bool IsMinToMax;
        bool IsShowOver;
        bool IsShowDummy;
        bool IsFirstLoad = false;

        HotKey _HotKey_Esc;
        HotKey _HotKey_F1;
        HotKey _hotkey_F4;
        HotKey _hotKey_F3;
        HotKey _HotKey_Print;
        HotKey _HotKey_F2;
        HotKey _HotKey_DeleteNum;

        public ViewOver1()
        {
            InitializeComponent();

            this.Title = "วิ่ง บน";
            Newload();
             _HotKey_Esc = new HotKey(Key.Escape, KeyModifier.None, OnCloseWindows);
             _HotKey_F1 = new HotKey(Key.F1, KeyModifier.None, Newload);
             _hotkey_F4 = new HotKey(Key.F4, KeyModifier.None, OnHotKeyShowDummy);
             _hotKey_F3 = new HotKey(Key.F3, KeyModifier.None, OnHotKeyShowOverLimit);
             _HotKey_Print = new HotKey(Key.F9, KeyModifier.None, PrintReport);
             _HotKey_F2 = new HotKey(Key.F2, KeyModifier.None, ShowMinToMax);
             _HotKey_DeleteNum = new HotKey(Key.Subtract, KeyModifier.None, DeleteNum);
        }
        public void UnregisterDelete()
        {
            if (_HotKey_DeleteNum != null)
            {
                _HotKey_DeleteNum.Unregister();
            }
        }
        public void RegisterDelete()
        {
            if (_HotKey_DeleteNum != null)
            {
                _HotKey_DeleteNum.Register();
            }
        }
        protected void DeleteNum(HotKey h)
        {
            UnregisterDelete();
            List<ShowNumberDTO> tempdel = new List<ShowNumberDTO>();
            tempdel.AddRange(showOver);
            tempdel.AddRange(showUnit);
            tempdel.AddRange(showDecimal);
            tempdel.AddRange(showHundred);
            deleteNUm deleteWin = new deleteNUm(tempdel, EnumLocal.NumberType.Over1);
            deleteWin.Owner = this;
            deleteWin.Show();
        }
        private void PrintReport(HotKey hotKey)
        {
            List<ShowNumberDTO> print_temp = new List<ShowNumberDTO>();
            print_temp.AddRange(showOver);
            print_temp.AddRange(showUnit);
            print_temp.AddRange(showDecimal);
            print_temp.AddRange(showHundred);


            Gobalsource.PrintReport(print_temp, EnumLocal.NumberType.Over1, LimitNum);

            Newload();
            
        }
        protected void ShowMinToMax(HotKey h)
        {
            IsFirstLoad = false;
            IsMinToMax = !IsMinToMax;
            LoadData();
        }
        protected void OnHotKeyShowDummy(HotKey h)
        {
            IsShowDummy = !IsShowDummy;
            LoadData();
        }
        protected void OnHotKeyShowOverLimit(HotKey h)
        {
            IsShowOver = !IsShowOver;
            LoadData();
        }

        private void Newload(HotKey hotKey)
        {
            Newload();
        }
        public void Newload()
        {
            IsFirstLoad = true;
            limit = PayService.GetLimit(EnumLocal.NumberType.Over1);

            LimitNum = limit.LimitBuy;
            minSt = limit.MinStart;
            minEn = limit.MinEnd;
            GetNumberShow();
            if (this.Owner != null)
            {
                ClientMain main = (ClientMain)this.Owner;
                main.ReloadSceen();
            }
        }
        private void OnCloseWindows(HotKey hotKey)
        {
            (Owner as ClientMain).UpdateData();
            this.Close();
        }
        public void GetNumberShow()
        {
            List<NumberDTO> dbNUmber = new List<NumberDTO>();

            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.Over1);
            dbNUmber = Gobalsource.GenDummy_1Runing(dbNUmber, EnumLocal.NumberType.Over1);
            showOver = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);

            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.Over1Unit);
            dbNUmber = Gobalsource.GenDummy_1Runing(dbNUmber, EnumLocal.NumberType.Over1Unit);
            showUnit = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);

            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.Over1Decimal);
            dbNUmber = Gobalsource.GenDummy_1Runing(dbNUmber, EnumLocal.NumberType.Over1Decimal);
            showDecimal = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);

            dbNUmber = Numberservice.get_numberShow(EnumLocal.NumberType.Over1Hundred);
            dbNUmber = Gobalsource.GenDummy_1Runing(dbNUmber, EnumLocal.NumberType.Over1Hundred);
            showHundred = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);

            //btnCut.IsEnabled = showOver.Count(c => c.IsOver == true) >= 1 || showUnit.Count(c => c.IsOver == true) >= 1 || showDecimal.Count(c => c.IsOver == true) >= 1 || showHundred.Count(c => c.IsOver == true) >= 1;
            labSum.Content = String.Format("ยอดขายรวม : {0}", showOver.Sum(s => s.Price) + showUnit.Sum(s => s.Price) + showDecimal.Sum(s => s.Price) + showHundred.Sum(s => s.Price));
            LoadData();

        }
        private void LoadData()
        {
            List<ShowNumberDTO> showOver_temp = new List<ShowNumberDTO>();
            List<ShowNumberDTO> showUnit_temp = new List<ShowNumberDTO>();
            List<ShowNumberDTO> showDecimal_temp = new List<ShowNumberDTO>();
            List<ShowNumberDTO> showHundred_temp = new List<ShowNumberDTO>();

            foreach (ShowNumberDTO showOverItem in showOver)
            {
                showOver_temp.Add(showOverItem.Clone());
            }

            foreach (ShowNumberDTO showUnitItem in showUnit)
            {
                showUnit_temp.Add(showUnitItem.Clone());
            }

            foreach (ShowNumberDTO showDecimalItem in showDecimal)
            {
                showDecimal_temp.Add(showDecimalItem.Clone());
            }

            foreach (ShowNumberDTO showHundredItem in showHundred)
            {
                showHundred_temp.Add(showHundredItem.Clone());
            }
          

            if (!IsShowDummy)
            {
                showOver_temp = showOver_temp.Where(w => w.IsZero == false).ToList();
                showUnit_temp = showUnit_temp.Where(w => w.IsZero == false).ToList();
                showDecimal_temp = showDecimal_temp.Where(w => w.IsZero == false).ToList();
                showHundred_temp = showHundred_temp.Where(w => w.IsZero == false).ToList();

            }
            if (IsShowOver)
            {
                showOver_temp = showOver_temp.Where(w => w.IsOver == true).ToList();
                showUnit_temp = showUnit_temp.Where(w => w.IsOver == true).ToList();
                showDecimal_temp = showDecimal_temp.Where(w => w.IsOver == true).ToList();
                showHundred_temp = showHundred_temp.Where(w => w.IsOver == true).ToList();

                showOver_temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);
                showUnit_temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);
                showDecimal_temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);
                showHundred_temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);

            }
            if (IsMinToMax && !IsFirstLoad)
            {
                showOver_temp = showOver_temp.OrderBy(n => n.Price).ToList();
                showUnit_temp = showUnit_temp.OrderBy(n => n.Price).ToList();
                showDecimal_temp = showDecimal_temp.OrderBy(n => n.Price).ToList();
                showHundred_temp = showHundred_temp.OrderBy(n => n.Price).ToList();
            }
            else
            {
                if (!IsFirstLoad)
                {
                    showOver_temp = showOver_temp.OrderByDescending(n => n.Price).ToList();
                    showUnit_temp = showUnit_temp.OrderByDescending(n => n.Price).ToList();
                    showDecimal_temp = showDecimal_temp.OrderByDescending(n => n.Price).ToList();
                    showHundred_temp = showHundred_temp.OrderByDescending(n => n.Price).ToList();
                }
            }

            listBoxPage1.ItemsSource = showOver_temp;
            listBoxPage2.ItemsSource = showHundred_temp;
            listBoxPage3.ItemsSource = showDecimal_temp;
            listBoxPage4.ItemsSource = showUnit_temp;
            labSum.Content = String.Format("{0:#,#}", showOver_temp.Sum(s => s.Price) + showUnit_temp.Sum(s => s.Price) + showDecimal_temp.Sum(s => s.Price) + showHundred_temp.Sum(s => s.Price));

        }
        private void btnCut_Click(object sender, RoutedEventArgs e)
        {
            tbl_ExportService exportService = new tbl_ExportService();
            exportService.ExportNumber(LimitNum, EnumLocal.NumberType.Over1, Gobalsource.UserNameLogin);
            this.GetNumberShow();
        }

        private void listBoxPage1_KeyDown(object sender, KeyEventArgs e)
        {
            //ListBox ls = sender as ListBox;
            //ShowNumberDTO select;
            //switch (e.Key)
            //{

            //    case Key.OemMinus:

            //         select = ls.SelectedItem as ShowNumberDTO;
            //        Numberservice.DeleteNumber(select, EnumLocal.NumberType.Over1, Gobalsource.UserNameLogin);
            //        this.GetNumberShow();
            //        break;
            //    case Key.Subtract:

            //         select = ls.SelectedItem as ShowNumberDTO;
            //        Numberservice.DeleteNumber(select, EnumLocal.NumberType.Over1, Gobalsource.UserNameLogin);
            //        this.GetNumberShow();
            //        break;

            //}
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            (Owner as ClientMain).UpdateData();
        }


    }
}
